package com.atguigu.springcloud;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author ：jjt8024
 * @date ：Created in 2021/02/22 14:41
 */
@RestController
@Slf4j
public class OrderController {

    //public static final String URL_SUFFIX = "http://localhost:8001";
    public static final String URL_SUFFIX = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment) {
         return restTemplate.postForObject(URL_SUFFIX+"/payment/create",payment,CommonResult.class);
//        return restTemplate.postForObject(URL_SUFFIX + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") String id) {
        log.info("++接收到的id++::"+id);
        return restTemplate.getForObject(URL_SUFFIX + "/payment/get/" + id, CommonResult.class);
    }

    @GetMapping("/consumer/payment/getForEntity/{id}")
    public CommonResult<Payment> getPayment2(@PathVariable("id") String id){
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(URL_SUFFIX+"/payment/get"+id, CommonResult.class);
        if(entity.getStatusCode().is2xxSuccessful()){
            return  entity.getBody();
        }else{
            return  new CommonResult<>(404,"操作失败！");
        }
    }
}
